package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.entity.JsonResult;
import com.csun.cmny.provider.pojo.ExpenseCtr;
import com.csun.cmny.provider.service.ExpenseCtrService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 类别管理
 * */
@RestController
@RequestMapping("/ExpenseCtr")
public class ExpenseCtrController extends BaseController {
    @Autowired
    private ExpenseCtrService expenseCtrService;

    /*
     * 这里的认证和当前登录的用户注解先预留，等完善了再进行修改
     * 1.判断该用户是否属于该机构人员
     * 2.基本的类别添加后需要到库里面将homeId置空（null），将isDelete置为1
     * */
    //添加类别
    @RequestMapping(value = "/addExpCtr", method = RequestMethod.POST)
    public Wrapper addExpCtr(@RequestBody ExpenseCtr expenseCtr) {
        //校验
        if (org.apache.commons.lang.StringUtils.isBlank(expenseCtr.getCategoryName())) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别名称不能为空",null);

        }
        if (org.apache.commons.lang.StringUtils.isBlank(expenseCtr.getExpensectrType())) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别不能为空",null);

        }
        if (org.apache.commons.lang.StringUtils.isBlank(expenseCtr.getDescription())) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别说明不能为空",null);

        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        expenseCtr.setHomeId(homeId);
        //判重——判断该类别是否已经存在
        ExpenseCtr expenseCtr1 = expenseCtrService.selectByExpCtr(expenseCtr);
        if (expenseCtr1 != null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该类别已经存在",null);
        }
        expenseCtr.setIsDelete(0);
        int n = expenseCtrService.insert(expenseCtr);
        if (n <= 0) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"添加失败",0);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"添加成功",expenseCtr.getExpensectrId());
    }

    //根据id删除类别
    @RequestMapping(value = "/delExpCtrById", method = RequestMethod.POST)
    public Wrapper delExpCtrById(Integer expensectrId) {
        if (expensectrId == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别id不能为空",null);
        }
        /*
        *删除前判断该类别是否是基本类别，如果是基本类别则不能删除；
        * 如果不是基本类别，判断该类别下是否有项目，如果有项目则不能删除
        */

        ExpenseCtr expenseCtr = expenseCtrService.selectById(expensectrId);
        if (expenseCtr == null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",expensectrId);
        }
        if (expenseCtr.getHomeId() == null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该类别是基本类别不能删除",expensectrId);
        }
        if (expenseCtr.getIsDelete() > 0) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该类别被使用中不能删除",expensectrId);
        }
        int n = expenseCtrService.delExpCtrById(expensectrId);
        if (n <= 0) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",expensectrId);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"删除成功",n);
    }

    //修改类别
    @RequestMapping(value = "/updateExpCtrById", method = RequestMethod.POST)
    public Wrapper updateExpCtrById(ExpenseCtr expenseCtr){
        //校验
        if (expenseCtr.getExpensectrId() == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别id不能为空",null);
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        if (org.apache.commons.lang.StringUtils.isNotBlank(expenseCtr.getCategoryName())) {
            ExpenseCtr expenseCtr1 = new ExpenseCtr();
            expenseCtr1.setCategoryName(expenseCtr.getCategoryName());
            expenseCtr1.setHomeId(homeId);
            //判断修改后的类别名称是否已经存在
            ExpenseCtr expenseCtr2 = expenseCtrService.selectByExpCtr(expenseCtr1);
            if (expenseCtr2 != null && expenseCtr2.getExpensectrId().intValue() != expenseCtr.getExpensectrId().intValue()) {
                return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该名称的类别已经存在",null);
            }
        }
        //修改
        int n = expenseCtrService.updateExpCtrById(expenseCtr);
        if (n <= 0) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"修改失败",0);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"修改成功",n);
    }

    //根据类别id查询类别
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(Integer expensectrId) {
        //校验
        if (expensectrId == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别id不能为空",expensectrId);
        }
        ExpenseCtr expenseCtr = expenseCtrService.selectById(expensectrId);
        if (expenseCtr == null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",expensectrId);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",expenseCtr);
    }
    //根据条件查询社区所有类别
    @RequestMapping(value = "/showAll", method = RequestMethod.POST)
    public Wrapper showAll(@RequestBody Map<String,Object> queryMap) {
        String pageNo = (String) queryMap.get("pageNo");
        String pageSize = (String) queryMap.get("pageSize");

        if (StringUtils.isEmpty(pageNo)) {
            pageNo = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }
        //当前登录用户获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        queryMap.put("homeId", homeId);
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        List<Map<String,Object>> mapList = expenseCtrService.showAll(queryMap);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(mapList);
        if (CollectionUtils.isEmpty(mapList)) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有查询到结果", new ArrayList<>());
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",pageInfo);
    }
    //根据类别id查询类别及类别下的所有项目或查询某个机构下的所有类别及类别下的项目
    //当类别id不为空时查询的是单个类别及类别下的项目；当类别id为空时查询的是机构所有类别及类别下的项目
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(ExpenseCtr expenseCtr) {
        Integer homeId = null;
        if (expenseCtr.getExpensectrId() == null) {
            //获取机构id
            LoginAuthDto loginAuthDto = getLoginAuthDto();
            homeId =loginAuthDto.getGroupId().intValue() ;
        }
        List<ExpenseCtr> expenseCtrList = expenseCtrService.selectOne(expenseCtr);
        if (CollectionUtils.isEmpty(expenseCtrList)) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"查询失败",expenseCtrList);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",expenseCtrList);
    }
}
